package com.platform.usercenter.service.impl;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import com.platform.common.core.exception.ServiceException;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.security.service.TokenService;
import com.platform.usercenter.domain.CalLeaderPlan;
import com.platform.usercenter.domain.dto.CalLeaderPlanDTO;
import com.platform.usercenter.enums.DayNameEnum;
import com.platform.usercenter.mapper.CalLeaderPlanMapper;
import com.platform.usercenter.service.ICalLeaderPlanService;
import com.platform.usercenter.utils.GenerateUtil;
import com.platform.usercenter.utils.PlanRuleUtils;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 领导排班计划主Service业务层处理
 *
 * @author platform
 * @date 2023-10-04
 */
@Service
public class CalLeaderPlanServiceImpl implements ICalLeaderPlanService {
	@Resource
	private CalLeaderPlanMapper calLeaderPlanMapper;

	@Resource
	private TokenService tokenService;

	/**
	 * 查询领导排班计划主
	 *
	 * @param id 领导排班计划主主键
	 * @return 领导排班计划主
	 */
	@Override
	public CalLeaderPlan selectCalLeaderPlanById( Long id ) {
		return calLeaderPlanMapper.selectCalLeaderPlanById( id );
	}

	/**
	 * 查询领导排班计划主列表
	 *
	 * @param calLeaderPlan 领导排班计划主
	 * @return 领导排班计划主
	 */
	@Override
	public List< CalLeaderPlan > selectCalLeaderPlanList( CalLeaderPlanDTO calLeaderPlan ) {
		return calLeaderPlanMapper.selectCalLeaderPlanList( calLeaderPlan );
	}

	/**
	 * 批量新增领导排班计划
	 *
	 * @param calLeaderPlan 领导排班计划主
	 * @return 结果
	 */
	@Override
	public int addBatch( CalLeaderPlanDTO calLeaderPlan ) throws ParseException {
		Set< String > collect=new HashSet<>( );
//		Long createBy=tokenService.getLoginUser( ).getUserid( );
		//获取值班规则
		String planRule=calLeaderPlan.getPlanRule( );
		List< String > localDates=PlanRuleUtils.calculateScheduleDates( calLeaderPlan.getPlanStartDate( ),calLeaderPlan.getPlanEndDate( ),planRule );
		//查询排班中是否有冲突的计划
		CalLeaderPlanDTO calLeaderPlan1=new CalLeaderPlanDTO( );
		calLeaderPlan1.setLeaderId( calLeaderPlan.getLeaderId( ) );
		List< CalLeaderPlan > calLeaderPlans1=calLeaderPlanMapper.selectCalLeaderPlanList( calLeaderPlan1 );
		if(CollectionUtils.isNotEmpty( calLeaderPlans1 )) {
			collect=calLeaderPlans1.stream( ).map( s->DateUtils.parseDateToStr( "yyyy-MM-dd",s.getPlanDate() ) ).collect( Collectors.toSet( ) );
		}
		for(String localDate: localDates) {
			if(!collect.contains( localDate )){
				calLeaderPlan.setCreateBy( Long.toString( 1L ) );
				calLeaderPlan.setCreateTime( DateUtils.getNowDate( ) );
				Integer countOfWeek=GenerateUtil.getCountOfWeek( DateUtils.parseDate( localDate ) );
				String dayName=DayNameEnum.getNameByValue( countOfWeek );
				calLeaderPlan.setDayName( dayName );
				calLeaderPlan.setPlanDate( DateUtils.parseDate( localDate ) );
				calLeaderPlanMapper.insertCalLeaderPlan(calLeaderPlan);
			}
		}
		return 1;
	}

	/**
	 * 新增单条排班计划
	 *
	 * @param calLeaderPlan
	 * @return
	 */
	@Override
	public int addSingle( CalLeaderPlanDTO calLeaderPlan ) {
		Long createBy=tokenService.getLoginUser( ).getUserid( );
		calLeaderPlan.setCreateBy( Long.toString( createBy ) );
		calLeaderPlan.setCreateTime( DateUtils.getNowDate( ) );
		Integer countOfWeek=GenerateUtil.getCountOfWeek( calLeaderPlan.getPlanDate( ) );
		String dayName=DayNameEnum.getNameByValue( countOfWeek );
		calLeaderPlan.setDayName( dayName );
		//查询是否有已存在计划
		List< Date > dateList=new ArrayList<>( );
		dateList.add( calLeaderPlan.getPlanDate( ) );
		Integer existCount=calLeaderPlanMapper.queryExistPlanByDate( dateList,calLeaderPlan.getLeaderId( ),calLeaderPlan.getLeaderDeptName() );
		if(existCount>0) {
			throw new ServiceException( "当前新增排班与现存排班冲突，请重新选择日期范围及周期！" );
		}
		return calLeaderPlanMapper.insertCalLeaderPlanSingle( calLeaderPlan );
	}

	/**
	 * 修改领导排班计划主
	 *
	 * @param calLeaderPlan 领导排班计划主
	 * @return 结果
	 */
	@Override
	public int updateCalLeaderPlan( CalLeaderPlan calLeaderPlan ) {
		Long updateBy=tokenService.getLoginUser( ).getUserid( );
		calLeaderPlan.setUpdateTime( DateUtils.getNowDate( ) );
		calLeaderPlan.setUpdateBy( Long.toString( updateBy ) );
		Integer countOfWeek=GenerateUtil.getCountOfWeek( calLeaderPlan.getPlanDate( ) );
		String dayName=DayNameEnum.getNameByValue( countOfWeek );
		calLeaderPlan.setDayName( dayName );
		//查询是否有已存在计划
		List< CalLeaderPlan > existRecord=calLeaderPlanMapper.selectCalLeaderPlanByDate( calLeaderPlan.getPlanDate( ),calLeaderPlan.getLeaderId( ) );
//		if(existRecord!=null && existRecord.size( )>0) {
//			for(CalLeaderPlan leaderPlan: existRecord) {
//				if(!leaderPlan.getId( ).equals(calLeaderPlan.getId( ))) {
//					throw new ServiceException( "当前新增排班与现存排班冲突，请重新选择日期范围及周期！" );
//				}
//			}
//		}
		return calLeaderPlanMapper.updateCalLeaderPlan( calLeaderPlan );
	}

	/**
	 * 批量删除领导排班计划主
	 *
	 * @param ids 需要删除的领导排班计划主主键
	 * @return 结果
	 */
	@Override
	public int deleteCalLeaderPlanByIds( List< Integer > ids ) {
		Long userid=tokenService.getLoginUser( ).getUserid( );
		return calLeaderPlanMapper.deleteCalLeaderPlanByIds( ids,userid );
	}

	/**
	 * 删除领导排班计划主信息
	 *
	 * @param id 领导排班计划主主键
	 * @return 结果
	 */
	@Override
	public int deleteCalLeaderPlanById( Long id ) {
		return calLeaderPlanMapper.deleteCalLeaderPlanById( id );
	}

	/**
	 * 生成插入计划数据模型
	 *
	 * @param dateList
	 * @param calLeaderPlan
	 * @return
	 */
	private List< CalLeaderPlan > generatePlanModel( List< Date > dateList,CalLeaderPlanDTO calLeaderPlan,Integer indexDay ) {
		List< CalLeaderPlan > modelList=new ArrayList<>( );
		Long leaderDeptId=calLeaderPlan.getLeaderDeptId( );
		String leaderDeptName=calLeaderPlan.getLeaderDeptName( );
		String leaderPhone=calLeaderPlan.getLeaderPhone( );
		String leaderName=calLeaderPlan.getLeaderName( );
		String remark=calLeaderPlan.getRemark( );
		Long leaderId=calLeaderPlan.getLeaderId( );
		Long createBy=tokenService.getLoginUser( ).getUserid( );
		String dayName=DayNameEnum.getNameByValue( indexDay );
		for(Date date: dateList) {
			CalLeaderPlan model=new CalLeaderPlan( );
			model.setLeaderDeptId( leaderDeptId );
			model.setLeaderDeptName( leaderDeptName );
			model.setLeaderPhone( leaderPhone );
			model.setLeaderName( leaderName );
			model.setLeaderId( leaderId );
			model.setCreateBy( Long.toString( createBy ) );
			model.setCreateTime( DateUtils.getNowDate( ) );
			model.setPlanDate( date );
			model.setDayName( dayName );
			model.setRemark( remark );
			modelList.add( model );
		}
		return modelList;
	}


	/**
	 * 批量新增领导排班计划
	 * @param calLeaderPlan 领导排班计划主
	 * @return 结果
	 */
	@Override
	public int addBatchByDateArr( CalLeaderPlanDTO calLeaderPlan ) throws ParseException {
		Set< String > collect=new HashSet<>( );
        List<String> dateArr=calLeaderPlan.getDateArr();
		CalLeaderPlanDTO calLeaderPlan1=new CalLeaderPlanDTO( );
		calLeaderPlan1.setLeaderId( calLeaderPlan.getLeaderId( ) );
		List< CalLeaderPlan > calLeaderPlans1=calLeaderPlanMapper.selectCalLeaderPlanList(calLeaderPlan1);
		if(CollectionUtils.isNotEmpty( calLeaderPlans1 )) {
			collect=calLeaderPlans1.stream( ).map( s->DateUtils.parseDateToStr( "yyyy-MM-dd",s.getPlanDate() ) ).collect( Collectors.toSet( ) );
		}
		Long createBy=tokenService.getLoginUser( ).getUserid( );
		calLeaderPlan.setCreateBy( Long.toString( createBy ) );
		calLeaderPlan.setCreateTime( DateUtils.getNowDate( ) );
        for(String planDate:dateArr){
			if(!collect.contains(planDate)){
				Integer countOfWeek=GenerateUtil.getCountOfWeek( DateUtils.parseDate( planDate ) );
				String dayName=DayNameEnum.getNameByValue( countOfWeek );
				calLeaderPlan.setDayName( dayName );
				calLeaderPlan.setPlanDate( DateUtils.parseDate( planDate ) );
				calLeaderPlanMapper.insertCalLeaderPlan(calLeaderPlan);
			}
		}
		return 1;
	}
}
